package com.css.dzj.zfjg.modules.creditassess.service.impl;


import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.common.util.ServiceUtil;
import com.css.dzj.zfjg.common.util.excel.ExcelData;
import com.css.dzj.zfjg.common.util.excel.ExportExcelUtils;
import com.css.dzj.zfjg.modules.creditassess.mapper.CreditassessMapper;
import com.css.dzj.zfjg.modules.creditassess.entity.JgLhjgCreditAssessEntity;
import com.css.dzj.zfjg.modules.creditassess.entity.QJgLhjgCreditAssessEntity;
import com.css.dzj.zfjg.modules.creditassess.service.CreditassessService;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;


/**
 * 企业信用评估信息serviceImpl
 */
@Service
public class CreditassessServiceImpl implements CreditassessService {

    @Autowired
    private CreditassessMapper creditassessMapper;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Autowired
    private ServiceUtil serviceUtil;

    /**
     * 添加企业信用评估信息详情
     * @param jgLhjgCreditAssessEntity
     */
    @Transactional
    @Override
    public void add(JgLhjgCreditAssessEntity jgLhjgCreditAssessEntity, HttpServletRequest request) {
        try {
            R r = serviceUtil.getUserInfoByRequest(request);
            /*if(r.getCode().equals("200")) {
                LoginUser userInfo =  (LoginUser)r.get("data");
                jgLhjgDffkEntity.setCreateUser(userInfo.getLoginName());
            }*/
            /*jgLhjgCreditAssessEntity.setColumnTime(new Date());*/
            creditassessMapper.save(jgLhjgCreditAssessEntity);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改信用线索企业详情
     * @param jgLhjgCreditAssessEntity
     */
    @Transactional
    @Override
    public void update(JgLhjgCreditAssessEntity jgLhjgCreditAssessEntity) {

    }

    /**
     * 通过id查找企业信用评估详情
     * @param creditAssessId
     * @return
     */
    @Override
    public JgLhjgCreditAssessEntity findById(String creditAssessId) {
        return creditassessMapper.findById(creditAssessId).get();
    }

    /**
     * 分页查询所有企业信用评估信息详情
     * @return
     */
    @Override
    public QueryResults<JgLhjgCreditAssessEntity> queryAll(Pageable pageable) {
        QJgLhjgCreditAssessEntity qJgLhjgCreditAssessEntity = QJgLhjgCreditAssessEntity.jgLhjgCreditAssessEntity;
        QueryResults<JgLhjgCreditAssessEntity> jgLhjgCreditAssessEntityQueryResults = jpaQueryFactory.selectFrom(qJgLhjgCreditAssessEntity)
                .orderBy(qJgLhjgCreditAssessEntity.columnTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        return jgLhjgCreditAssessEntityQueryResults;
    }
    /**
     * 多条件分页查询信用线索企业详情
     * @param pageable
     * @param enterpriseName
     * @param enterpriseIdType
     * @param enterpriseType
     * @return
     */
    @Override
    public QueryResults<JgLhjgCreditAssessEntity> listJgLhjgCreditAssessEntity(Pageable pageable, String enterpriseName, String enterpriseIdType, String enterpriseType) {
        QJgLhjgCreditAssessEntity qJgLhjgCreditAssessEntity = QJgLhjgCreditAssessEntity.jgLhjgCreditAssessEntity;
        Predicate predicate = qJgLhjgCreditAssessEntity.isNotNull().or(qJgLhjgCreditAssessEntity.isNull());
        predicate = (enterpriseName == null || enterpriseName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qJgLhjgCreditAssessEntity.enterpriseName.like("%" + enterpriseName + "%"));
        predicate = (enterpriseIdType == null || enterpriseIdType.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qJgLhjgCreditAssessEntity.enterpriseIdType.like("%" + enterpriseIdType + "%"));
        predicate = (enterpriseType == null || enterpriseType.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qJgLhjgCreditAssessEntity.enterpriseType.eq(enterpriseType));
        QueryResults<JgLhjgCreditAssessEntity> jgLhjgCreditAssessEntityQueryResults = jpaQueryFactory.selectFrom(qJgLhjgCreditAssessEntity)
                .where(predicate)
                .orderBy(qJgLhjgCreditAssessEntity.enterpriseMark.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        return jgLhjgCreditAssessEntityQueryResults;
    }


    /**
     * 通过企业证件号码查询企业信用评估信息
     * @param enterpriseCreditCode
     * @return
     */
    @Override
    public JgLhjgCreditAssessEntity queryByIdNumber(String enterpriseCreditCode) {
        QJgLhjgCreditAssessEntity qJgLhjgCreditAssessEntity = QJgLhjgCreditAssessEntity.jgLhjgCreditAssessEntity;
        return jpaQueryFactory.selectFrom(qJgLhjgCreditAssessEntity)
                .where(qJgLhjgCreditAssessEntity.enterpriseCreditCode.eq(enterpriseCreditCode))
                .fetchOne();
    }

    /**
     * 物理删除企业信用评估信息
     * @param jgLhjgCreditAssessEntity
     */
    @Override
    public void delete(JgLhjgCreditAssessEntity jgLhjgCreditAssessEntity) {
        try {
            creditassessMapper.delete(jgLhjgCreditAssessEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 判断企业证件号码重复
     * @param enterpriseCreditCode
     * @return
     */
    private boolean isRepeatByEnterpriseIdNumber(String enterpriseCreditCode) {
        QJgLhjgCreditAssessEntity q = QJgLhjgCreditAssessEntity.jgLhjgCreditAssessEntity;
        return jpaQueryFactory.selectFrom(q)
                .where(q.enterpriseCreditCode.eq(enterpriseCreditCode))
                .fetchCount() > 0;
    }

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<Map> getAll(){
        return creditassessMapper.getAll();
    }


    /**
     * 根据任务编码查询企业信息
     *
     */
    @Override
    public JgLhjgCreditAssessEntity findByTaskNo(String taskNo) {
        QJgLhjgCreditAssessEntity q = QJgLhjgCreditAssessEntity.jgLhjgCreditAssessEntity;
        return jpaQueryFactory.selectFrom(q).where(q.taskNo.eq(taskNo)).fetchOne();
    }
    /**
     * 查询数量
     * @return
     */
    @Override
    public Integer gettotalnum() {
        QJgLhjgCreditAssessEntity qJgLhjgCreditAssessEntity = QJgLhjgCreditAssessEntity.jgLhjgCreditAssessEntity;
        List<JgLhjgCreditAssessEntity> list = jpaQueryFactory.selectFrom(qJgLhjgCreditAssessEntity)
                .orderBy(qJgLhjgCreditAssessEntity.columnTime.desc())
                .fetch();
        Integer result;
        if(list.size()>0){
            result = list.size();
        }else{
            result = 0;
        }
        return result;
    }
    /**
     *根据ids查询信用评估信息列表
     * @param ids
     * @return
     */
    @Override
    public List<JgLhjgCreditAssessEntity> queryByIds(String ids) {
        if (com.alibaba.druid.util.StringUtils.isEmpty(ids)) {
            throw new RuntimeException("ids为空!");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        List<JgLhjgCreditAssessEntity> jgLhjgCreditAssessEntities = creditassessMapper.findAllById(idList);
        return jgLhjgCreditAssessEntities;
    }
    /**
     * 企业信用评估信息导出Excel
     * @param response
     * @param jgLhjgCreditAssessEntities
     */
    @Override
    public void exportCreditAssessToExcel(HttpServletResponse response, List<JgLhjgCreditAssessEntity> jgLhjgCreditAssessEntities) {
        if (jgLhjgCreditAssessEntities == null || jgLhjgCreditAssessEntities.size() < 0) {
            throw new RuntimeException("企业信息为空");
        }
        // 创建导出实体
        ExcelData data = new ExcelData();
        data.setName("企业信用评估信息表");
        /**
         * 设置Excel标题
         */
        ArrayList<String> titles = new ArrayList<>();
        titles.add("企业名称");
        titles.add("企业分类");
        titles.add("企业证件类型");
        titles.add("企业证件号码");
        titles.add("企业分数");
        titles.add("评估日期");
        data.setTitles(titles);
        /**
         * 处理行数据
         */
        ArrayList<List<Object>> rows = new ArrayList<>();
        for (JgLhjgCreditAssessEntity zfjgJgLhjgCreditAssessEntity : jgLhjgCreditAssessEntities) {
            ArrayList<Object> row = new ArrayList<>();
                row.add(zfjgJgLhjgCreditAssessEntity.getEnterpriseName());
                row.add(zfjgJgLhjgCreditAssessEntity.getEnterpriseType());
                row.add(zfjgJgLhjgCreditAssessEntity.getEnterpriseIdType());
                row.add(zfjgJgLhjgCreditAssessEntity.getEnterpriseCreditCode());
                row.add(zfjgJgLhjgCreditAssessEntity.getEnterpriseMark());
                row.add(zfjgJgLhjgCreditAssessEntity.getColumnTime());
                rows.add(row);
        }
        data.setRows(rows);
        try {
            // 导出
            ExportExcelUtils.exportExcel(response, "企业信用评估信息表.xlsx", data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 根据参数导出数据到Excel
     * @param enterpriseName
     * @param enterpriseMark
     * @param enterpriseType
     */
    @Override
    public List<JgLhjgCreditAssessEntity> queryByParams(String enterpriseName, String enterpriseMark, String enterpriseType) {
        List<JgLhjgCreditAssessEntity> jgLhjgCreditAssessEntities = new ArrayList<JgLhjgCreditAssessEntity>();
        QJgLhjgCreditAssessEntity qZfjgJgLhjgCreditAssessEntity = QJgLhjgCreditAssessEntity.jgLhjgCreditAssessEntity;
        Predicate predicate = qZfjgJgLhjgCreditAssessEntity.isNotNull().or(qZfjgJgLhjgCreditAssessEntity.isNull());
        predicate = (enterpriseName == null || enterpriseName.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qZfjgJgLhjgCreditAssessEntity.enterpriseName.like("%" + enterpriseName + "%"));
        predicate = (enterpriseMark == null || enterpriseMark.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qZfjgJgLhjgCreditAssessEntity.enterpriseMark.eq(enterpriseMark));
        predicate = (enterpriseType == null || enterpriseType.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qZfjgJgLhjgCreditAssessEntity.enterpriseType.eq(enterpriseType));
        jgLhjgCreditAssessEntities = jpaQueryFactory.selectFrom(qZfjgJgLhjgCreditAssessEntity)
                .where(predicate)
                .orderBy(qZfjgJgLhjgCreditAssessEntity.columnTime.desc())
                .fetch();
        return jgLhjgCreditAssessEntities;
    }
    /**
     * 分页查询企业信用评估信息列表
     * @param
     * @return
     */
    @Override
    public List creditInfo(String enterpriseName,String enterpriseMark1,String enterpriseMark2,String enterpriseType) {
        QJgLhjgCreditAssessEntity qJgLhjgCreditAssessEntity = QJgLhjgCreditAssessEntity.jgLhjgCreditAssessEntity;
        Predicate predicate = qJgLhjgCreditAssessEntity.isNotNull().or(qJgLhjgCreditAssessEntity.isNull());
        predicate = ("".equals(enterpriseName) || enterpriseName == null) ? predicate : ExpressionUtils.and(predicate, qJgLhjgCreditAssessEntity.enterpriseName.like("%"+enterpriseName+"%"));
        predicate = ("".equals(enterpriseType) || enterpriseType == null) ? predicate : ExpressionUtils.and(predicate, qJgLhjgCreditAssessEntity.enterpriseType.like(enterpriseType));
        if ((StringUtils.isEmpty(enterpriseMark1)) && (!StringUtils.isEmpty(enterpriseMark2))) {
            try{
                predicate = ExpressionUtils.and(predicate, qJgLhjgCreditAssessEntity.enterpriseMark.goe(enterpriseMark2));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if ((!StringUtils.isEmpty(enterpriseMark1)) && (StringUtils.isEmpty(enterpriseMark2))) {
            try{
                predicate = ExpressionUtils.and(predicate, qJgLhjgCreditAssessEntity.enterpriseMark.loe(enterpriseMark1));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if ((!StringUtils.isEmpty(enterpriseMark1)) && (!StringUtils.isEmpty(enterpriseMark2))) {
            try{
                predicate = ExpressionUtils.and(predicate, qJgLhjgCreditAssessEntity.enterpriseMark.between(enterpriseMark1, enterpriseMark2));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return jpaQueryFactory.selectFrom(qJgLhjgCreditAssessEntity)
                .where(
                        predicate
                )
                .orderBy(qJgLhjgCreditAssessEntity.columnTime.desc())
                .fetch();
    }

}
